package sun.it.web;

import org.apache.commons.beanutils.BeanUtils;
import sun.it.domain.User;
import sun.it.service.UserService;
import sun.it.service.impl.UserServiceimpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/updatePasswordServlet")
public class UpdatePasswordServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        User user=new User();
        HttpSession session = request.getSession();
        String opassword = request.getParameter("opassword");
        String npassword = request.getParameter("npassword");
        String enpassword = request.getParameter("enpassword");
        String username = (String) request.getSession().getAttribute("username");
        UserService service =new UserServiceimpl();
       if(username!=null){
           if(npassword!=null&&enpassword!=null&&npassword.equals(enpassword)) {
//这个地方可能写错
               String password = service.LookupPas(username);
               if (password != null && password.equals(opassword)) {
                   user.setUsername(username);
                   user.setPassword(npassword);
                   //返回更新的语句
                   int update = service.UpdateUP(user);
                   System.out.println(update);
                       if(update==1){
                          response.sendRedirect("index.jsp");
                       }else { //判断语句错误，密码

                           response.sendRedirect("chanp.jsp");
                       }
               } else {
                   System.out.println("密码错误");
                   request.setAttribute("ch_error", "旧密码错误，请重试");
                   request.getRequestDispatcher("chanp.jsp").forward(request, response);
               }
           }else {
               System.out.println("密码不一样");
               request.setAttribute("check_error", "两次密码不一样");
               response.sendRedirect("chanp.jsp");
           }
       }else{

           session.setAttribute("error_msg","请登录");
           response.sendRedirect("login.jsp");
       }

//        service.
//        service.UpdatePassword(password);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
    }
}
